package kr.pe.johnkim.chattingday.mobile.rest.dao;

import kr.pe.johnkim.chattingday.mobile.domain.ChattingDayGathering;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.util.List;

/**
 * Chatting Day 채팅방 관련 DAO 메소드들을 구현하는 클래스 이다.
 * 
 * @author : johnkim <cale9797@gmail.com / Bettersoft Inc,.>
 * @Date : 12. 6. 4
 */
public class ChattingDayGatheringDaoImpl extends SqlMapClientDaoSupport implements ChattingDayGatheringDao {

    // SLF4J Logging
	private Logger logger = LoggerFactory.getLogger(ChattingDayGatheringDaoImpl.class);

    /**
     * 채팅방을 개설 한다.
     *
     * @param chattingDayGathering
     * @return
     */
    @Override
    public boolean registerGathering(ChattingDayGathering chattingDayGathering) {
        logger.info("registerGathering");

        boolean result;

        try {
            getSqlMapClientTemplate().insert("CHATTINGDAY_GATHERINGS.registerGathering", chattingDayGathering);
            result = true;
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    /**
     * 채팅방 정보를 가져 온다.
     *
     * @param chattingDayGathering
     * @return
     */
    @Override
    public ChattingDayGathering selectGatheringInformation(ChattingDayGathering chattingDayGathering) {
        logger.info("selectGatheringInformation");

        return (ChattingDayGathering)getSqlMapClientTemplate().queryForObject("CHATTINGDAY_GATHERINGS.selectGatheringInformation", chattingDayGathering);
    }

    /**
     * 검색한 채팅방 목록을 가져 온다.
     *
     * @param chattingDayGathering
     * @return
     */
    @Override
    public List<ChattingDayGathering> selectSearchGatheringList(ChattingDayGathering chattingDayGathering) {
        logger.info("selectSearchGatheringList");

        return this.getSqlMapClientTemplate().queryForList("CHATTINGDAY_GATHERINGS.selectSearchGatheringList", chattingDayGathering);
    }

    /**
     * 채팅방 정보를 수정 한다.
     *
     * @param chattingDayGathering
     * @return
     */
    @Override
    public boolean updateGatheringInformation(ChattingDayGathering chattingDayGathering) {
        logger.info("updateGatheringInformation");

        boolean result;

        try {
            getSqlMapClientTemplate().update("CHATTINGDAY_GATHERINGS.updateGatheringInformation", chattingDayGathering);
            result = true;
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    /**
     * 채팅방을 비활성화 한다.(개설자만 가능하다.)
     * <p/>
     * 1. 개설자가 채팅방에서 나갈 경우.
     * 2. 개설자가 인위적으로 비활성화 할 경우.
     * <p/>
     * 참여자와 메시지도 모두 비활성화 한다.
     *
     * @param chattingDayGathering
     * @return
     */
    @Override
    public boolean updateDeactivateGathering(ChattingDayGathering chattingDayGathering) {
        logger.info("updateDeactivateGathering");

        boolean result;

        try {
            getSqlMapClientTemplate().update("CHATTINGDAY_GATHERINGS.updateDeactivateGathering", chattingDayGathering);
            result = true;
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    /**
     * 개설된 채팅방의 총 갯수를 가져 온다.
     *
     * @param chattingDayGathering
     * @return
     */
    @Override
    public ChattingDayGathering selectGatheringCount(ChattingDayGathering chattingDayGathering) {
        logger.info("selectGatheringCount");

        return (ChattingDayGathering)getSqlMapClientTemplate().queryForObject("CHATTINGDAY_GATHERINGS.selectGatheringCount", chattingDayGathering);
    }
}
